<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="" prop="aId">
                    <el-input v-model="formData.aId" placeholder="请输入" />
                </el-form-item>
                <el-form-item label="模块" prop="module">
                    <el-input v-model="formData.module" placeholder="请输入模块" />
                </el-form-item>
                <el-form-item label="控制器" prop="controller">
                    <el-input v-model="formData.controller" placeholder="请输入控制器" />
                </el-form-item>
                <el-form-item label="方法" prop="action">
                    <el-input v-model="formData.action" placeholder="请输入方法" />
                </el-form-item>
                <el-form-item label="描述" prop="intro">
                    <el-input
                        v-model="formData.intro"
                        placeholder="请输入描述"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="用户id" prop="uid">
                    <el-input v-model="formData.uid" placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="账号" prop="account">
                    <el-input
                        v-model="formData.account"
                        placeholder="请输入账号"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="模块下系统id" prop="aid">
                    <el-input v-model="formData.aid" placeholder="请输入模块下系统id" />
                </el-form-item>
                <el-form-item label="请求真实IP" prop="ip">
                    <el-input v-model="formData.ip" placeholder="请输入请求真实IP" />
                </el-form-item>
                <el-form-item label="参数" prop="params">
                    <el-input
                        v-model="formData.params"
                        placeholder="请输入参数"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
                <el-form-item label="结果" prop="result">
                    <el-input
                        v-model="formData.result"
                        placeholder="请输入结果"
                        type="textarea"
                        :autosize="{ minRows: 4, maxRows: 6 }"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  fashi_log_actionEdit, fashi_log_actionAdd, fashi_log_actionDetail } from '@/api/fashi_log_action'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑操作日志' : '新增操作日志'
})

const formData = reactive({
    id: '',
    aId: '',
    module: '',
    controller: '',
    action: '',
    intro: '',
    uid: '',
    account: '',
    aid: '',
    ip: '',
    params: '',
    result: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    aId: [
        {
            required: true,
            message: '请输入',
            trigger: ['blur']
        }
    ],
    module: [
        {
            required: true,
            message: '请输入模块',
            trigger: ['blur']
        }
    ],
    controller: [
        {
            required: true,
            message: '请输入控制器',
            trigger: ['blur']
        }
    ],
    action: [
        {
            required: true,
            message: '请输入方法',
            trigger: ['blur']
        }
    ],
    intro: [
        {
            required: true,
            message: '请输入描述',
            trigger: ['blur']
        }
    ],
    uid: [
        {
            required: true,
            message: '请输入用户id',
            trigger: ['blur']
        }
    ],
    account: [
        {
            required: true,
            message: '请输入账号',
            trigger: ['blur']
        }
    ],
    aid: [
        {
            required: true,
            message: '请输入模块下系统id',
            trigger: ['blur']
        }
    ],
    ip: [
        {
            required: true,
            message: '请输入请求真实IP',
            trigger: ['blur']
        }
    ],
    params: [
        {
            required: true,
            message: '请输入参数',
            trigger: ['blur']
        }
    ],
    result: [
        {
            required: true,
            message: '请输入结果',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await fashi_log_actionEdit(data) : await fashi_log_actionAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await fashi_log_actionDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
